<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="">
      </div>
      <!-- login-from -->
      <el-form :rules="loginFromRules" :model="loginFrom" ref="loginFormRef" label-width="0px" class="login_from_box">
        <el-form-item prop='username'>
          <!-- 账号 -->
          <el-input v-model="loginFrom.username" prefix-icon='iconfont icon-user'></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <!-- 密码 -->
          <el-input v-model="loginFrom.password" prefix-icon='iconfont icon-3702mima' type="password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button @click="resetForm" type="info">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 表单数据绑定的对象
      loginFrom: {
        username: 'admin',
        password: 'admin1234'
      },
      //  用户登录验证数据的对象
      loginFromRules: {
        // 用户名验证
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        // 密码验证
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm () {
      this.$refs.loginFormRef.resetFields()
    },
    // 登录前的再一次验证
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          console.log('校验失败')
          return
        }
        console.log('校验成功 ')
        // 登录状态
        const { data: res } = await this.$http.post('login', this.loginFrom)
        if (res.meta.status !== 200) {
          this.$message.error('登录失败')
          return
        }
        this.$message.success('登录成功')
        console.log(res.data)
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })

      // promise 函数
      // this.$refs.loginFormRef.validate().then(() => {
      //   console.log('验证成功')
      // }).catch((err) => {
      //   console.log(err)
      // })
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
.login_container {
  height: 100%;
  background-color: #2b4b6b;
  position: relative;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .avatar_box {
      position: absolute;
      height: 130px;
      width: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      background-color: #fff;
      left: 50%;
      transform: translate(-50%, -50%);
      box-shadow: 0 0 10px #ddd;
      padding: 10px;
      img {
        display: block;
        height: 100%;
        width: 100%;
        border-radius: 50%;
        background-color: #ccc;
      }
    }
  }
}
// 登录输入框
.login_from_box {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
}
// a按钮
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
